package cn.migu.music.hangupsms.common.config;

import cn.migu.music.common.service.async.ContextAwareThreadPoolTaskExecutor;
import com.alibaba.ttl.threadpool.TtlExecutors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @version 1.0 created by duanhao@migu.cn on 2022/5/5 2:57 下午
 */
@Configuration
public class ExecutorConfig {
    @Autowired
    private HangUpSmsConfig hangUpSmsConfig;

    @Bean
    public Executor hangupSmsAsyncExecutor() {
        ContextAwareThreadPoolTaskExecutor contextAwareThreadPoolTaskExecutor = new ContextAwareThreadPoolTaskExecutor();
        // 设置线程池大小，队列大小等属性
        contextAwareThreadPoolTaskExecutor.setCorePoolSize(hangUpSmsConfig.getAsyncCorePoolSize());
        contextAwareThreadPoolTaskExecutor.setMaxPoolSize(hangUpSmsConfig.getAsyncMaxPoolSize());
        contextAwareThreadPoolTaskExecutor.setQueueCapacity(hangUpSmsConfig.getAsyncqueueSize());
        contextAwareThreadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        contextAwareThreadPoolTaskExecutor.initialize();
        return TtlExecutors.getTtlExecutor(contextAwareThreadPoolTaskExecutor);
    }

    @Bean
    public Executor hangupViewVrbtAsyncExecutor() {
        return hangupSmsAsyncExecutor();
    }
}
